package com.ws.service.impl;

import com.github.pagehelper.Page;
import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.User;
import com.ws.repository.UserRepository;
import com.ws.service.UserService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/23 15:41
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserRepository userRepository;

    @Override
    public PaasPage<User> getUserPage(Pageable pageable, String name, String gender, Long departmentId,String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        User user = new User();
        user.setDepartmentId(departmentId);
        user.setName(name);
        user.setGender(gender);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        user.setStatusCd(statusCd);
        Page<User> userPage = userRepository.getUserPage(user);
        return PageUtil.transformPage(userPage);
    }

    @Override
    public User saveUser(User user) {
        user.setStatusCd(CommonConstant.ADD_STATUS_CD);
        user.setCreateTime(new Date());
        userRepository.saveUser(user);
        return user;
    }

    @Override
    public User getUserBasicInfo(Long userId) {
        return userRepository.getUserBasicInfo(userId);
    }

    @Override
    public void delUser(Long userId) {
        userRepository.delUser(userId);
    }

    @Override
    public User updateUser(Long userId, User user) {
        user.setUserId(userId);
        userRepository.updateUser(user);
        return user;
    }

    @Override
    public List<User> getUserList(String name, String gender, Long departmentId, String statusCd) {
        User user = new User();
        user.setDepartmentId(departmentId);
        user.setName(name);
        user.setGender(gender);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        user.setStatusCd(statusCd);
        List<User> userList = userRepository.getUserList(user);
        return userList;
    }
}
